package com.alibaba.citrus.ots.platform.enhance.functions.strategy.factory;

import com.alibaba.citrus.ots.common.exception.FunctionException;
import com.alibaba.citrus.ots.common.util.DateUtils;
import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.functions.strategy.entity.StrategyEntity;
import com.alibaba.citrus.ots.platform.enhance.functions.utils.ConverterUtils;
import com.alibaba.citrus.ots.platform.enhance.repository.DimensionWriteRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.StrategyQueryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.model.dto.*;
import com.epoch.app.otsplatformenhance.sdo.StrategySDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;


public class StrategyFactory {

    private static StrategyQueryRepository strategyQueryRepository;

    static {
        strategyQueryRepository = SpringContextUtil.getBean(StrategyQueryRepository.class);
    }

    public static StrategyEntity createStrategy(StrategyWriteCreateRequest request) {
        StrategySDO strategySDO = ConverterUtils.createStrategyConverter(request);
        StrategyEntity strategyEntity = StrategyEntity.of(strategySDO);
        return strategyEntity;
    }

    public static StrategyEntity bulid(FbGenerateOrderSwitchRequest request) {
        StrategySDO strategySDO = ConverterUtils.convertToSdoFbRequest(request);
        StrategyEntity strategyEntity = StrategyEntity.of(strategySDO);
        return strategyEntity;
    }

    public static StrategyEntity deleteStrategy(StrategyWriteDeleteRequest request) {
        StrategyQueryByIdRequest strategyQueryByIdRequest = new StrategyQueryByIdRequest();
        if (!StringUtils.isEmpty(request.getId())) {
            strategyQueryByIdRequest.setId(request.getId());
            Result<StrategyQueryByIdResponse> result = strategyQueryRepository.queryById(strategyQueryByIdRequest);
            if (null != result.getResult()) {
                StrategySDO strategySDO = ConverterUtils.deleteStrategyConverter(result.getResult());
                StrategyEntity strategyEntity = StrategyEntity.of(strategySDO);
                return strategyEntity;
            }
        }
        throw new RuntimeException("策略id不能为空");
    }

    public static StrategyEntity updateStrategy(StrategyWriteUpdateRequest request) {
        StrategySDO strategySDO = ConverterUtils.updateStrategyConverter(request);
        StrategyEntity strategyEntity = StrategyEntity.of(strategySDO);
        return strategyEntity;
    }

    public static StrategyEntity saveStrategy(SaveStrategyRequest saveStrategyRequest) {
        StrategyQueryByIdRequest strategyQueryByIdRequest = new StrategyQueryByIdRequest();
        strategyQueryByIdRequest.setId(saveStrategyRequest.getId());
        Result<StrategyQueryByIdResponse> result = strategyQueryRepository.queryById(strategyQueryByIdRequest);
        if (Objects.isNull(result) || Objects.isNull(result.getResult()) || Objects.isNull(result.getResult().getStrategySDO()) || CollectionUtils.isEmpty(result.getResult().getStrategySDO().getDimensionSDOList())) {
            throw new FunctionException("OTS-06-001-00-15-009");
        }
        StrategySDO strategySDO = ConverterUtils.deleteStrategyConverter(result.getResult());
        strategySDO.setStatus(saveStrategyRequest.getStatus());
        strategySDO.setOrganizationCode(saveStrategyRequest.getOrganizationCode());
        strategySDO.setShare(saveStrategyRequest.getShare());
        strategySDO.setName(saveStrategyRequest.getName());
        strategySDO.setDailyBeginTime(saveStrategyRequest.getDailyBeginTime());
        strategySDO.setDailyEndTime(saveStrategyRequest.getDailyEndTime());
        if (CollectionUtils.isNotEmpty(saveStrategyRequest.getStrategyValid()) && saveStrategyRequest.getStrategyValid().size() >= 2){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATETIME_FORMAT);
            try {
                strategySDO.setValidBeginTime(sdf.parse(saveStrategyRequest.getStrategyValid().get(0)));
                strategySDO.setValidEndTime(sdf.parse(saveStrategyRequest.getStrategyValid().get(1) + " 23:59:59"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        strategySDO.getDimensionSDOList().forEach(dimensionSDO -> {
            dimensionSDO.setStatus(saveStrategyRequest.getStatus());
        });
        StrategyEntity strategyEntity = StrategyEntity.of(strategySDO);
        return strategyEntity;
    }

    public static StrategyEntity createFirstReply(StrategySDO strategySDO) {
        return StrategyEntity.of(strategySDO);
    }
}
